//测试Tag
// #define TEST_ADMOB

using System.Collections.Generic;
using System.Globalization;
using GoogleMobileAds.Api;
using GoogleMobileAds.Ump.Api;
using UnityEngine;

namespace HWGames.Bundles.Internal.GDPR {
    public class ConsentMgr : Singleton<ConsentMgr> {

        private bool? _isInitialized;
        public bool CanRequestAds =>
    ConsentInformation.ConsentStatus == ConsentStatus.Obtained ||
    ConsentInformation.ConsentStatus == ConsentStatus.NotRequired;

        public void Init() {
#if TEST_ADMOB
            ConsentInformation.Reset();
#endif
            MobileAds.SetiOSAppPauseOnBackground(true);
            MobileAds.RaiseAdEventsOnUnityMainThread = true;
            // If we can request ads, we should initialize the Google Mobile Ads Unity plugin.
            if (CanRequestAds) {
                InitializeGoogleMobileAds();
            }
            // Ensures that privacy and consent information is up to date.
            InitializeGoogleMobileAdsConsent();
        }

        private void InitializeGoogleMobileAdsConsent() {
            if (IsShowTime()) {
                Debug.Log("Google Mobile Ads gathering consent.");
                GatherConsent();
            }
            else {
                Debug.Log("Not Time Show Ads gathering consent.");
                InitializeGoogleMobileAds();
            }
        }

        private void GatherConsent() {
            Debug.Log("Google Mobile Ads gathering consent.");
#if TEST_ADMOB

            var debugSettings = new ConsentDebugSettings {
                // Geography appears as in EEA for debug devices.
                DebugGeography = DebugGeography.EEA,
                TestDeviceHashedIds = new List<string> { "CDA45E543F95EE3E414A77EC3DBF55AB" }
            };
#endif
            // Set tag for under age of consent.
            // Here false means users are not under age of consent.
            ConsentRequestParameters request = new ConsentRequestParameters {
                TagForUnderAgeOfConsent = false,
#if TEST_ADMOB
                ConsentDebugSettings = debugSettings,
#endif
            };

            // Check the current consent information status.
            ConsentInformation.Update(request, OnConsentInfoUpdated);
        }

        private void OnConsentInfoUpdated(FormError updateError) {
            if (updateError != null) {
                OnConsentComplete(updateError.Message);
                return;
            }
            if (CanRequestAds) {
                OnConsentComplete(null);
                return;
            }
            // If the error is null, the consent information state was updated.
            // You are now ready to check if a form is available.
            ConsentForm.LoadAndShowConsentFormIfRequired((FormError showError) => {
                if (showError != null) {
                    // Form showing failed.
                    OnConsentComplete(showError.Message);
                }
                // Form showing succeeded.
                else {
                    OnConsentComplete(null);
                }
            });
        }

        private void OnConsentComplete(string error) {
            if (error != null) {
                Debug.LogError("Failed to gather consent with error: " + error);
            }
            else {
                Debug.Log("Google Mobile Ads consent updated: "
                    + ConsentInformation.ConsentStatus);
            }
            InitializeGoogleMobileAds();
        }

        private void InitializeGoogleMobileAds() {
            if (_isInitialized.HasValue) {
                return;
            }
            _isInitialized = false;
            AdManager.Instance.Init();
            HWLog.Log("HWSDKBundle", "Initializing Ads");
        }

        private bool IsShowTime() {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
            System.DateTime currentTime = System.DateTime.UtcNow.Date;
            System.DateTime targetDate = new System.DateTime(2024, 1, 14);
            int comparisonResult = currentTime.CompareTo(targetDate);
            return comparisonResult >= 0;
        }
    }
}
